﻿using Newtonsoft.Json;
using System;
using System.IO;
using System.Windows.Forms;

namespace QWManage.Model
{
    public class DateConfigInfo
    {
        public DateTime From { set; get; } = DateTime.Now.Date.AddYears(-1);
        /// <summary>
        /// 主体
        /// </summary>
        public DateTime To { set; get; } = DateTime.Now.Date;

        public int Start
        {
            get
            {
                DateTime dt = new DateTime(From.Year, From.Month, From.Day, 0, 0, 0);
                long timeStamp = new DateTimeOffset(dt).ToUnixTimeSeconds();
                return (int)timeStamp;
            }
        }
        public int End
        {
            get
            {
                DateTime dt = new DateTime(To.Year, To.Month, To.Day, 23, 59, 59);
                long timeStamp = new DateTimeOffset(dt).ToUnixTimeSeconds();
                return (int)timeStamp;
            }
        }
    }

    public class DateConfigMgr
    {

        public static DateConfigInfo Get()
        {
            DateConfigInfo obj = null;
            try
            {
                string fn = Path.Combine(Application.StartupPath, "mydatecfg.dll");
                if (File.Exists(fn))
                {
                    string json = File.ReadAllText(fn);
                    obj = JsonConvert.DeserializeObject<DateConfigInfo>(json);
                    return obj;
                }
            }
            catch { }
            if (obj == null) { obj = new DateConfigInfo(); }
            return obj;
        }

        public static void Set(DateConfigInfo info)
        {
            try
            {
                string fn = Path.Combine(Application.StartupPath, "mydatecfg.dll");
                {
                    string json = JsonConvert.SerializeObject(info);
                    File.WriteAllText(fn, json);
                }
            }
            catch { }
        }
    }
}